You would expect to use:
Car
class
Professional programmers carefully design
the classes they need before any coding
is done.
With well-designed classes
programming is much easier and the
program has fewer bugs.
Object oriented design consists of deciding what classes are
needed, what data they will hold, and how they will behave.
All these decisions are documented (written up) and then examined.
If something doesn't look right, it is fixed before any programming is done.
Let us do that with our Car
class.
CarA class that calculates miles per gallon.
Variables
- double startMiles; // Starting odometer reading
- double endMiles; // Ending odometer reading
- double gallons; // Gallons of gas used between the readings
Constructors
- Car( double startOdo, double endingOdo, double gallons )
Creates a new instance of a Car object with the starting and ending odometer readings and the number of gallons of gas consumed.Methods
- double calculateMPG()
calculates and returns the miles per gallon for the car.
Look at the parameter list for the constructor:
Car( double startOdo, double endingOdo, double gallons );
This says that the constructor must be called with three items
of data: three double
precision values.
Could a main()
method create a Car
object?